package com.zuoyy.modules.system.query;

import com.zuoyy.common.enums.StatusEnum;
import com.zuoyy.modules.system.domain.Post;
import com.zuoyy.modules.system.query.BaseQuery;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.jpa.domain.Specification;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

/**
 * @author 
 */
@Getter
@Setter
public class PostQuery extends BaseQuery implements Specification<Post> {

    private Integer status;

    @Override
    public Predicate toPredicate(Root<Post> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
        List<Predicate> list = new ArrayList<Predicate>();
        list.add(cb.notEqual(root.get("status").as(Integer.class), StatusEnum.DELETE.code));
        if (Objects.nonNull(status)) {
            list.add(cb.equal(root.get("status").as(Integer.class), status));
        }

        Predicate[] p = new Predicate[list.size()];
        return cb.and(list.toArray(p));
    }

}